package com.jmu.medicineknows.utils;

import com.jmu.medicineknows.entity.Menu;
import lombok.experimental.UtilityClass;
import java.util.ArrayList;
import java.util.List;
@UtilityClass
public class MenuUtil {
    public List<Menu> generateMenu(List<Menu> resourceList){
        List<Menu> menuList=new ArrayList<>();
        for (Menu childrenDynamicMenu:resourceList){
            //只要不是顶级目录都要寻找其父目录
            if(childrenDynamicMenu.getParentId()!=0){
                //把其添加到其父目录里
                for (Menu parentDynamicMenu:resourceList){
                    //找到其父级目录
                    if(childrenDynamicMenu.getParentId().equals(parentDynamicMenu.getId())){
                        List<Menu> children = parentDynamicMenu.getChildren();
                        if(children==null){
                            children=new ArrayList<Menu>();
                        }
                        children.add(childrenDynamicMenu);
                        parentDynamicMenu.setChildren(children);
                    }
                }
            }
        }
        //只返回父级菜单即可
        for (Menu dynamicMenuVO:resourceList){
            if(dynamicMenuVO.getParentId()==0){
                menuList.add(dynamicMenuVO);
            }
        }
        return menuList;
    }
}

